import { domain } from '@/little/config';
import { Http } from 'lwu-request';
import { loginByCode } from '@/api';
const http = new Http({
    baseUrl: {
        // 开发环境域名，此处仅为演示
        dev: domain.dev_api,
        // 生产环境域名，此处仅作为演示
        pro: domain.pro_api
    },
    // before: (args) => {
    //     console.log('before args', args);
    // },
    // after: (args) => {
    //     console.log("after args", args)
    // },
    // taskIdValue: (data, options) => {
    //     console.log("data", data, "options", options, this)
    // },
    debug: true,
    autoRefreshToken: true,
    tokenExpiredCode: 401,
    xhrCode: 200,
    xhrCodeName: 'code',
    requestSuccessResponseMsgName: 'message',
    tokenValue: () => {
        return new Promise((resolve, _) => {
            uni.getStorage({
                key: 'token',
                success: ({ data }) => {
                    resolve(`Bearer ${data}`);
                },
                fail: (_) => {
                    resolve('');
                }
            });
        });
    },
    refreshTokenHandle: () => {
        return new Promise((resolve, _) => {
            uni.login({
                provider: 'weixin',
                success: (res) => {
                    loginByCode(res.code).then((res) => {
                        const data = res.data;
                        const token = uni.setStorageSync('token', data.token);
                        token && resolve(data.token);
                    });
                }
            });
        });
    },
    buildQueryString: (params) => {
        const args = uni.$u.queryParams(params, true, 'repeat');
        return args;
    },
    apiErrorInterception: (data, _args) => {
        console.log('_args', _args);
        uni.$u.toast(data.message || '失败，请稍后重试！');
        return new Promise(() => {});
    },
    errorHandleByCode: (code, errMsg) => {
        switch (code) {
            case 200:
                break;
            case 403:
                uni.$u.toast(errMsg || '未授权访问，请确认是否拥有权限！');
                break;
            case 404:
                uni.$u.toast(errMsg || '请求资源不存在！');
                break;
            case 500:
            case 501:
            case 502:
            case 503:
                uni.$u.toast(errMsg || '服务异常，请予管理员联系！');
                break;
            default:
                uni.$u.toast('请求服务异常，请稍后重试！');
        }
    }
});
export const fetch = http;
